﻿using System.Linq;
using System.Xml.Linq;

namespace Personal.BuildAsSilverlight4
{
    public static class XmlUtilities
    {
        public static void CopyTagValue(XDocument sourceFile, XDocument destinationFile, string attributeName)
        {
            var valueToBeCopied = sourceFile.Descendants(attributeName).FirstOrDefault();
            if (valueToBeCopied != null)
                destinationFile.Descendants(attributeName).FirstOrDefault().SetValue(valueToBeCopied.Value);
        }

        public static void AppendTags(XDocument sourceFile, XDocument destinationFile, string sourceAttributeName, string destinationAttributeName)
        {
            destinationFile.Descendants(destinationAttributeName).FirstOrDefault().Add(sourceFile.Descendants(sourceAttributeName));
        }

        public static void UnionTags(XDocument sourceFile, XDocument destinationFile, string sourceAttributeName, string destinationAttributeName)
        {
            destinationFile.Descendants(sourceAttributeName).FirstOrDefault().Add(sourceFile.Descendants(sourceAttributeName));
            var items = destinationFile.Descendants(sourceAttributeName).Union(sourceFile.Descendants(sourceAttributeName));
            var itemArray = items.ToArray();
            destinationFile.Descendants(sourceAttributeName).Remove();
            destinationFile.Descendants(destinationAttributeName).FirstOrDefault().Add(itemArray);
        }
    }
}
